from django.db import models
from django.contrib.auth.models import User

from django.forms import ModelForm
# from django import forms


class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)

    # Other fields here
    job_position = models.CharField(max_length=32, default='Asistent Farmacist')

    # To indicate to the 'django.contrib.auth' app that this model is
    # the user profile model for a given site,
    # AUTH_PROFILE_MODULE = 'auth.UserProfile'
    # must be present in the settings file.


class LoginForm(ModelForm):
    class Meta:
        model = User
        fields = ('username', 'password')


# class LoginForm(forms.Form):
#     username = forms.CharField(max_length=50);
#     password = forms.PasswordInput()


# The method User.get_profile() does not create a profile if one does not exist.
# Register a handler for the User model's django.db.models.signals.post_save signal and,
# in the handler, if created is True, create the associated user profile:
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

models.signals.post_save.connect(create_user_profile, sender=User)
